<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Modules
 * @package    User
 * @subpackage Controllers
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * User settings controller
 * 
 * @category   LOSC Modules
 * @package    User
 * @subpackage Controllers
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class User_SettingsController extends Zym_Controller_Action_Abstract 
{
    /**
     * User settings
     *
     * @return void
     */
    public function indexAction()
    {
        $user = App_User::getCurrentUser();
        
        $form = $this->getForm();
        $form->populate($user->getData());
        $this->view->form = $form;
        
        $locale = $user->locale;
        
        $selectors = array('gregorian', 'format', 'wide');
        $list = Zend_Locale_Data::getList($locale, 'month', $selectors);
        $this->view->monthNames = array_values($list);
        
        $selectors = array('gregorian', 'format', 'wide');
        $list = Zend_Locale_Data::getList($locale, 'day', $selectors);
        $this->view->dayNames = array_values($list);
        
        $selectors = array('gregorian', 'stand-alone', 'narrow');
        $list = Zend_Locale_Data::getList($locale, 'day', $selectors);
        $this->view->dayNamesMin = array_values($list);
    }
    
    /**
     * Saves user settings
     *
     * @return void
     */
    public function saveAction()
    {
        $request = $this->getRequest();
        
        if (!$request->isPost()) {
            $this->notice('Saving requires request to be POST.');
            $this->_goto('index');
        }
        
        $user = clone App_User::getCurrentUser();
        
        $form = $this->getForm();
        $form->setUser($user);
        
        if ($form->isValid($_POST)) {
            try {
                $user = $form->getUser();
                $user->save();
                
                $this->notice('Settings saved successfully.');
                
                if ($request->isXmlHttpRequest()) {
                    $form->populate($user->getData());
                } else {
                    $this->_goto('index');
                }
            } catch (Doctrine_Exception $de) {
                $msg = 'Database error (%s).';
                $msg = $this->translate(array($msg, $de->getMessage()));
                $this->notice($msg);
            } catch (Losc_Exception $le) {
                $msg = 'Application error (%s).';
                $msg = $this->translate(array($msg, $le->getLocalizedMessage()));
                $this->notice($msg);
            } catch (Exception $e) {
                $msg = 'Internal error (%s).';
                $msg = $this->translate(array($msg, $e->getMessage()));
                $this->notice($msg);
            }
        }
        
        if ($form) {
            $this->view->form = $form->render($this->view);
        } else {
            $this->view->form = $form;
        }
        
        $locale = $user->locale;
        
        $selectors = array('gregorian', 'format', 'wide');
        $list = Zend_Locale_Data::getList($locale, 'month', $selectors);
        $this->view->monthNames = array_values($list);
        
        $selectors = array('gregorian', 'format', 'wide');
        $list = Zend_Locale_Data::getList($locale, 'day', $selectors);
        $this->view->dayNames = array_values($list);
        
        $selectors = array('gregorian', 'stand-alone', 'narrow');
        $list = Zend_Locale_Data::getList($locale, 'day', $selectors);
        $this->view->dayNamesMin = array_values($list);
        
        $this->getHelper('ViewRenderer')->setRender('index');
    }
    
    /**
     * Returns form
     *
     * @return User_Form_User
     */
    public function getForm()
    {
        $saveUrl = $this->getHelper('Url')->url(array(
            'action' => 'save'
        ));
        
        $form = $this->getHelper('Form')->create('User');
        $form->setAction($saveUrl);
        
        return $form;
    }
    
    /**
     * Translates a message
     *
     * @param  string|array $message
     * @return string
     */
    public function translate($message)
    {
        $t = $this->getHelper('Translator');
        
        if (is_array($message)) {
            $msg = array_shift($message);
            $message = vsprintf($t->translate($msg), $message);
        } else {
            $message = $t->translate($message);
        }
        
        return $message;
    }
    
    /**
     * Adds a notice message
     *
     * @param  string $message
     * @return void
     */
    public function notice($message)
    {
        $message = $this->translate($message);
        
        $fm = $this->getHelper('FlashMessenger');
        $fm->addMessage($message);
    }
}
